#include"client_head.h"

//消息端口
#define MSG_PORT 50021

//用户登入登出端口
#define Login_Out_PORT 50022

int comfd; //发送消息
int logfd; //登入登出
struct sockaddr_in srvAddr;

//接收其他客户端的消息
void *recvMsg(void *arg)
{
    int n;
    char *buf = calloc(1,1024);
    char flag[2];
    char msg[1024];
    while(1)
    {
        bzero(buf,1024);
        bzero(flag,2);
        bzero(msg,1024);
        n = read(comfd,buf,1024);
        // buf类型:type:sendid:sendname:recvid:msg:size
        if(n == -1)
		{
			perror("读取网络数据失败");
			break;
		}
		if(n == 0)
		{
			printf("对端已关闭\n");
			break;
		}
        printf("buf:%s\n",buf);
        char *type = strtok(buf,":");
        printf("type:%s\n",type);
        if(strcmp(type,"0") == 0)
        {
            //解析出发送的消息
            char *send_id = strtok(NULL,":");
            char *sendname = strtok(NULL,":");
            char *recv_id = strtok(NULL,":");
            char *msg = strtok(NULL,":");
            printf("\n收到:%s\n", msg);
        }
        else if(strcmp(type,"1") == 0)
        {
            char *send_id = strtok(NULL,":");
            char *sendname = strtok(NULL,":")
            char *recv_id = strtok(NULL,":");
            char *msg = strtok(NULL,":");
            char *size = strtok(NULL,":");
            int fsize = atoi(size);
            // printf("filename:%s filesize:%d\n",msg,atoi(size));
            if(fsize == 0)
            {
                
            }
            //响应接收
            // sprintf(msg,"")
            // write() 
        }else if(strcmp(type,"2") == 0)
        {
            char *send_id = strtok(NULL,":");
            int id = atoi(send_id);
            char buf[512];
            sprintf(buf,"2:%d:yes",id);
            write(comfd,buf,strlen(buf));

        }
    }
}

//登入注册
int login(log_info info)
{
    char msg[32];
    bzero(msg,32);
    sendto(logfd, &info, sizeof(info), 0, (struct sockaddr *)&srvAddr, sizeof(srvAddr));
    recvfrom(logfd,msg,32,0,NULL,NULL);
    printf("msg:%s\n",msg);
    if(strcmp(msg,"success") == 0)
    {
        return 1;
    }
    else if(strcmp(msg,"regist fail") == 0)
    {
        return -2;
    }
    else if(strcmp(msg,"fail") == 0)
    {
        return 2;
    }
    else if(strcmp(msg,"regist success")==0)
    {
        return -1;
    }
}

//接收OOB数据
void recvOOB(int sig)
{
    char oob;
    recv(comfd, &oob, 1, MSG_OOB); //接收对端的OOB带外数据
	printf("指定的ID不存在,或对方已下线。\n");
}

//登录界面
void login_display(void)
{
    printf("\n---------------------------------\n");
    printf("--------------主界面--------------\n");
    printf("--------------1.登录--------------\n");
    printf("--------------2.注册--------------\n");
    printf("请选择你的操作:\n");
}



int main(int argc, char const *argv[])
{
    logfd = socket(AF_INET,SOCK_DGRAM,IPPROTO_UDP);
    comfd = socket(AF_INET,SOCK_STREAM,0);

    //确定服务器地址
    srvAddr.sin_family = AF_INET;
    srvAddr.sin_addr.s_addr = inet_addr("192.168.3.211");

    //登入登出注册
    srvAddr.sin_port = htons(Login_Out_PORT);
    int check;
    log_info info;
LOOP:
    //登录界面
    while(1)
    {
        login_display();
        scanf("%d",&check);
        bzero(&info,sizeof(info));
        int flag;
        if(check == 1) //登录
        {
            strcpy(info.logmsg,"in");
            printf("请输入你的账户:");
            scanf("%u",&info.id);
            printf("请输入你的密码:");
            scanf("%s",info.password);
            flag = login(info);
            if(flag == 1)
            {
                break;
            }
            else if(flag == 2)
            {
                continue;
            }
        }
        else if(check == 2)//注册
        {
            strcpy(info.logmsg,"regist");
            printf("请输入你的账户:");
            scanf("%d",&info.id);
            printf("请输入你的名字:");
            scanf("%s",info.username);
            printf("请输入你的密码:");
            scanf("%s",info.password);
            flag = login(info);
        }
    }


    //聊天端口
    srvAddr.sin_port =htons(MSG_PORT);
    if(connect(comfd,(struct sockaddr*)&srvAddr,sizeof(srvAddr)) == 0)
    {
        printf("连接成功\n");
    }
    sleep(1);

    //若私聊对端不存在，服务器将发送一个OOB数据过来提醒
    signal(SIGURG,recvOOB);
    fcntl(comfd,F_SETOWN,getpid());

    //

    //接收其他客户端的消息
    pthread_t tid;
    pthread_create(&tid,NULL,recvMsg,NULL);

    //发送消息给其他客户端
    char msg2[32];
    int recvid;
    char buf[512];
    int type = 0; //0:普通消息   1:文件
    int filesize = 0;
    char flag[2] ={0};
    while(1)
    {
        bzero(msg2,32);
        bzero(buf,512);
        bzero(flag,2);
        printf("请输入对方id:");
        scanf("%d",&recvid);
        printf("请输入要发送的消息:");
        scanf("%s",msg2);
        sprintf(buf,"0:%d:%s",recvid,msg2);
        write(comfd,buf,strlen(buf));
        // printf("请输入yes/no\n");

        // write(comfd,buf,strlen(buf));
        //文件测试 
        // printf("请输入要传输的文件:");
        // scanf("%s",msg2);
        // struct stat statbuf;
        // if(lstat(argv[1], &statbuf) < 0)
        // {
        //     perror("stat() fail");
        //     return -1;
        // }
        // filesize = statbuf.st_size;
        // sprintf(buf,"0:%d:%s:%d",recvid,msg2,filesize);
        // write(comfd,buf,strlen(buf));

        // //接收信号
        // read(comfd,flag,2);
        // if(strcmp(flag,"y") == 0)
        // {
        //     //开始接收
        //     int fd = open(msg2,O_RDONLY);
        //     if(fd < 0) {
        //         perror("open fail");
        //         return -1;
        //     }

        //     long int total_size = 0;
        //     int nread = 0;
        //     char buf[1024];
        //     while(1)
        //     {
        //         bzero(buf, 1024);
        //         nread = read(fd, buf, 1024);
        //         total_size += nread;

        //         write(comfd, buf, nread);
        //         if(total_size == file.filesize)
        //             break;
        //     }
        // }   
        // else if(strcmp(flag,"n")){
        //     //对方拒收
        //     continue;
        // }

        // sleep(1);
    }
    return 0;
}
